package com.business.action.admin.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.business.bean.system.MenuBean;
import com.business.bean.system.UserBean;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.util.WebUtils;

@Controller
@RequestMapping("/admin/menu")
public class MenuAction {

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/list.do")
	public List<MenuBean> list(HttpServletRequest request, Service service, String menuID) {
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(MenuBean.class);
		query.createSql("parentID", menuID);
		query.addSql(" ORDER BY sequence asc ");
		return dao.list();
	}

	/**
	 * 根据用户角色获取用户被授权的所有菜单
	 */
	@ResponseBody
	@RequestMapping("/biz/getMenu.do")
	public PageBean getMenu(HttpServletRequest request, Service service) {
		UserBean user = (UserBean) WebUtils.getUserBeanCache(request);
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		String role = user.getRoleID();
		if (role == null) {
			page.setSuccess(false);
			page.setMessage("此用户还没有分配权限...");
			return page;
		}
		query.setEntityClass(MenuBean.class);
		query.setSQL(service.getSql("sys.menu.author.user.menu"));
		query.addParameter('%' + role + '%');
		query.addParameter('%' + role + '%');
		query.addSql(" ORDER BY sequence asc ");
		// query.setSQL("select * from SYS_MENU");
		List<MenuBean> list = dao.list();
		Map<String, MenuBean> map = new HashMap<>();
		for (MenuBean m : list) {
			map.put(m.getMenuID(), m);
		}
		MenuBean root = null;
		for (MenuBean m : list) {
			if (m.getParentID() == null) {
				root = m;
			} else {
				MenuBean p = map.get(m.getParentID());
				if (p != null) {
					p.getChildren().add(m);
				}
			}
		}
		page.setData(root.getChildren());
		return page;
	}

	/**
	 * 
	 */
	@ResponseBody
	@RequestMapping("/save.do")
	public PageBean save(HttpServletRequest request, Service service, MenuBean bean) {
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(MenuBean.class);
		bean.setInstID("nt.wison");
		String id = bean.getMenuID();
		if (!bean.getPath().startsWith("/page")) {
			bean.setPath("/page" + bean.getPath());
		}
		if (id == null || id.equals("")) {
			String pid = bean.getParentID();
			query.setSQL("select max(menuID) as menuID from SYS_MENU where parentID=?");
			query.addParameter(pid);
			MenuBean dic = dao.get();
			// 每一个点占4位，根结点也占4位长
			if (dic == null || dic.getMenuID() == null) {
				id = pid + "0001";
			} else {
				String d1 = dic.getMenuID();
				String s1 = d1.substring(0, d1.length() - 4);
				String s2 = d1.substring(d1.length() - 4);
				s2 = Integer.parseInt(s2) + 1 + "";
				s2 = "000" + s2;
				s2 = s2.substring(s2.length() - 4);
				id = s1 + s2;
			}
			bean.setMenuID(id);
			query.createInsertSql(bean);
		} else {
			query.createUpdateSql(bean, "authorRole,forbidRole");
		}
		if (dao.update() > 0) {
			res.setData(bean);
			res.setMessage("保存成功！");
		} else {
			res.setSuccess(false);
			res.setMessage("保存失败！");
		}
		return res;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	public PageBean delete(HttpServletRequest request, Service service, String menuID) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.createDeleteSql(MenuBean.class, menuID);
		if(!dao.execute()){
			page.setSuccess(false);
			page.setMessage("删除失败！");
			return page;
		}
		return page;
	}

}
